//
//  AutoTextTableViewCell.m
//  OMS
//
//  Created by goldeney on 14-10-20.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "AutoTextTableViewCell.h"
#import "UILabel+StringFrame.h"
#import "NSDictionary+DataFormat.h"

@implementation AutoTextTableViewCell

- (void)setDataArray:(NSArray*)dictionary
{
    [self drawDataToDictionary:dictionary];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.opaque = YES;
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    if (self.isChangeWidth) {
        frame.origin.x += 10;
        frame.size.width = SuperWidth - 20;
    }
    [super setFrame:frame];
}

//绘制一排文字
- (void)drawRowCellView:(NSString*)attribute content:(NSString*)content
{
    CGRect contetnFrame = self.contentView.frame;
    CGSize contetnSize = contetnFrame.size;
    
    UILabel *attributeLabel = [UILabel new];
    attributeLabel.text = attribute;
    attributeLabel.font = [UIFont fontWithName:nil size:15];
    attributeLabel.textColor = [UIColor darkGrayColor];
    attributeLabel.numberOfLines = 0;
    CGSize attributeSize = [attributeLabel boundingRectWithSize:CGSizeMake(0, 0)];
    attributeLabel.frame = CGRectMake(10.0f, contetnSize.height + 5.0f, attributeSize.width, attributeSize.height);
    
    CGFloat attributeUseWidth = attributeLabel.frame.origin.x + attributeLabel.frame.size.width;
    CGFloat width = contetnSize.width - attributeUseWidth - 5;
    
    UILabel *contentLabel = [UILabel new];
    contentLabel.text = content;
    contentLabel.font = [UIFont fontWithName:nil size:15];
    contentLabel.textColor = [UIColor darkGrayColor];
    contentLabel.numberOfLines = 0;
    CGSize contentSize = [contentLabel boundingRectWithSize:CGSizeMake(width, 0)];
    contentLabel.frame = CGRectMake(attributeUseWidth - 5, contetnSize.height + 5.0f, contentSize.width, contentSize.height);
    
    [self.contentView addSubview:attributeLabel];
    [self.contentView addSubview:contentLabel];
    
    if (attributeSize.height>contentSize.height){
        contetnFrame.size.height += attributeSize.height + 5;
    } else {
        contetnFrame.size.height += contentSize.height + 5;
    }
    self.contentView.frame = contetnFrame;
}

//多拍文字绘制
- (void)drawDataToDictionary:(NSArray*)array
{
    CGRect contetnFrame = self.contentView.frame;
    contetnFrame.size.height = 5;
    self.contentView.frame = contetnFrame;
    
    for (int i=0; i<array.count; i++) {
        NSDictionary *dictionary = [array objectAtIndex:i];
        NSArray *keys = [dictionary allKeys];
        id key = [keys firstObject];
        id value = [dictionary objectFormatStringForKey:key];
        [self drawRowCellView:key content:value];
    }
    
    CGRect contetnLastFrame = self.contentView.frame;
    contetnLastFrame.size.height += 10;
    self.contentView.frame = contetnLastFrame;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



@end
